package com.ss.android.article.base.feature.feed.presenter;

import com.bytedance.common.utility.Logger;
import com.bytedance.frameworks.baselib.network.http.exception.HttpResponseException;
import com.ss.android.article.base.feature.app.a.ArticleConstants;
import com.ss.android.article.base.feature.app.b.DBHelper;
import com.ss.android.article.base.feature.detail.model.ArticleDetail;
import com.ss.android.article.base.feature.detail.model.ArticleInfo;
import com.ss.android.article.base.feature.model.CellRef;
import com.ss.android.article.base.feature.model.Article;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.common.util.json.JsonUtil;
import com.ss.android.common.util.UrlBuilder;
import com.ss.android.http.legacy.HttpHeader;
import com.ss.android.http.legacy.a.NameValueImpl;
import com.ss.android.http.legacy.a.ResponseHeaders;
import com.ss.android.image.model.ImageInfo;
import com.ss.android.model.AbsArticle;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public final class AnswerQueryUtils {
    private static final List c;
    private static List a;
    private static List b;

    static {
        a = null;
        b = null;
        ArrayList v0 = new ArrayList();
        c = ((List) v0);
        ((List) v0).add("ib.snssdk.com");
    }

    public static void a(List arg0) {
        a = arg0;
    }

    public static ArticleInfo a(DBHelper arg8, Article arg9, String arg10, String arg11, String arg12, String arg13) throws Throwable {
        ArticleInfo v1 = null;
        if (arg9 != null) {
            ArrayList v2 = new ArrayList();
            v2.add(new NameValueImpl("ansid", String.valueOf(arg9.groupId)));
            String v3 = "scope";
            if (arg10 == null) {
                arg10 = "";
            }

            v2.add(new NameValueImpl(v3, arg10));
            v3 = "enter_from";
            if (arg11 == null) {
                arg11 = "";
            }

            v2.add(new NameValueImpl(v3, arg11));
            v3 = "api_param";
            if (arg12 == null) {
                arg12 = "";
            }

            v2.add(new NameValueImpl(v3, arg12));
            v3 = "gd_ext_json";
            if (arg13 == null) {
                arg13 = "";
            }

            v2.add(new NameValueImpl(v3, arg13));
            int v3_1 = 20480;
            String v0 = ArticleConstants.wendaAnswerInformationUrl;
            if (!android.support.design.a.isTextEmpty(v0) && (v0.contains("/wenda/v1/"))) {
                UrlBuilder v4 = new UrlBuilder(v0);
                v4.addParam("wd_version", "1");
                v0 = v4.build();
            }

            v0 = NetworkUtils.executePost(v3_1, v0, ((List) v2));
            if (android.support.design.a.isTextEmpty(v0)) {
                return v1;
            }

            JSONObject v7 = new JSONObject(v0);
            if (!a(v7)) {
                Logger.d("AnswerQueryUtils", "get article info error: " + v7);
                return v1;
            }

            v1 = new ArticleInfo(arg9.groupId, arg9.itemId, (byte) 0);
            v1.a(v7, false);
            if (!v1.delete) {
                return v1;
            }

            if (arg8 == null) {
                return v1;
            }

            arg8.deleteArticle(((AbsArticle) arg9));
        }

        return v1;
    }

    private static boolean a(JSONObject arg2) {
        boolean v0 = arg2.optInt("err_no", -1) == 0 ? true : false;
        return v0;
    }

    public static ArticleDetail a(DBHelper arg18, AbsArticle arg19, long arg20, String arg22, boolean arg23) throws Throwable {
        JSONObject v13;
        JSONObject v2_6;
        String v4_1;
        if (arg19 == null) {
            ArticleDetail v2 = null;
            return v2;
        }

        List v2_1 = b;
        if (arg23) {
            v2_1 = a;
        }

        if (v2_1 == null || (v2_1.isEmpty())) {
            v2_1 = c;
        }

        int v3 = 0;
        Iterator v9 = v2_1.iterator();
        while (v9.hasNext()) {
            Object v2_2 = v9.next();
            int v8 = v3 + 1;
            if (v8 >= 3) {
                return null;
            }

            StringBuilder v3_1 = new StringBuilder("http://");
            v3_1.append(((String) v2_2));
            if (arg23) {
                v3_1.append("/wenda/v1/answer/detail/answer_id/".replace("answer_id", String.valueOf(arg20)));
            } else {
                v3_1.append("/article/content/lite/14/1/");
                v3_1.append(arg19.groupId).append("/").append(arg19.itemId).append("/").append(arg19.aggrType).append("/");
            }

            UrlBuilder v4 = new UrlBuilder(v3_1.toString());
            v4.addParam("group_id", arg19.groupId);
            String v3_2 = "api_param";
            String v2_3 = android.support.design.a.isTextEmpty(arg22) ? "" : arg22;
            v4.addParam(v3_2, v2_3);
            v3_2 = v4.build();
            ResponseHeaders v6 = new ResponseHeaders();
            try {
                long v10 = System.currentTimeMillis();
                v4_1 = NetworkUtils.doGet(-1, v3_2, true, null, v6, null);
                if (System.currentTimeMillis() - v10 > 3000) {
                    ArticleQueryThread.reportEvent("loading", arg19.groupId, null);
                }
            } catch (Exception v2_4) {
                if ((v2_4 instanceof HttpResponseException)) {
                    int v2_5 = ((HttpResponseException) v2_4).getStatusCode();
                    JSONObject v3_3 = new JSONObject();
                    v3_3.put("error_type", 0);
                    v3_3.put("status", v2_5);
                    ArticleQueryThread.reportEvent("error", arg19.groupId, v3_3);
                }

                v3 = v8;
                continue;
            }

            if (android.support.design.a.isTextEmpty(v4_1)) {
                v2_6 = new JSONObject();
                v2_6.put("error_type", 1);
                v2_6.put("error_msg", "missing response");
                ArticleQueryThread.reportEvent("error", arg19.groupId, v2_6);
                v3 = v8;
                continue;
            }

            try {
                v2_6 = new JSONObject(v4_1);
                if (!a(v2_6)) {
                    Logger.w("AnswerQueryUtils", "get item detail error: " + v4_1);
                    v2_6 = new JSONObject();
                    v2_6.put("error_type", 1);
                    v2_6.put("error_msg", "api message error");
                    ArticleQueryThread.reportEvent("error", arg19.groupId, v2_6);
                    v3 = v8;
                    continue;
                }

                v13 = v2_6.getJSONObject("data");
                if (v13 == null) {
                    v3 = v8;
                    continue;
                }
            } catch (JSONException v2_7) {
                try {
                    v2_7.printStackTrace();
                    ArticleQueryThread.a(null, v3_2, v4_1);
                    ArticleQueryThread.a(v3_2, v4_1);
                    v3 = v8;
                    continue;
                } catch (Exception v2_4) {
                    v2_6 = new JSONObject();
                    v2_6.put("error_type", 1);
                    v2_6.put("error_msg", "missing field data");
                    ArticleQueryThread.reportEvent("error", arg19.groupId, v2_6);
                    v3 = v8;
                    continue;
                }
            }

            boolean v2_8 = v13.optInt("delete") > 0 ? true : false;
            String v7 = v13.optString("content");
            long v4_2 = System.currentTimeMillis();
            ArticleDetail v14 = new ArticleDetail();
            v14.itemId = arg19.groupId;
            v14.content = v7;
            v14.timestamp = v4_2;
            v14.delete = v2_8;
            v3_2 = "";
            v4_1 = "";
            JSONArray v5 = v13.optJSONArray("image_detail");
            if (v5 != null) {
                try {
                    v14.imageDetail = ImageInfo.parseImageList(v5, false);
                    v3_2 = v5.toString();
                } catch (Exception v5_1) {
                }
            }

            v5 = v13.optJSONArray("thumb_image");
            if (v5 != null) {
                try {
                    ImageInfo.parseImageList(v5, false);
                    v4_1 = v5.toString();
                } catch (Exception v5_1) {
                }
            }

            v14.appendExtraData(v13);
            int v15 = v13.optInt("article_type");
            int v5_2 = (v14.itemId & 131072) <= 0 || v15 != 1 ? 0 : 1;
            if (!v14.delete && (android.support.design.a.isTextEmpty(v7)) && v5_2 == 0) {
                if (v14.galleryImageInfoList == null) {
                    v3 = v8;
                    continue;
                } else if (!v14.galleryImageInfoList.isEmpty()) {

                } else {
                    v3 = v8;
                    continue;
                }
            }

            Article v7_1 = new Article(arg19.groupId, arg19.itemId, arg19.aggrType);
            if (arg23) {
                JsonUtil.parseJSONObject(v13, v7_1);
                v7_1.delete = v2_8;
                if (v7_1.delete) {
                    v7_1.commentCount = 0;
                } else {
                    ArrayList v2_9 = new ArrayList();
                    v2_9.add(new CellRef("", v7_1.behotTime, v7_1));
                    arg18.insertArticleList(((List) v2_9));
                }

                v2_3 = v13.optString("answer_abstract");
                if (android.support.design.a.isTextEmpty(v2_3)) {
                    v2_3 = v7_1._abstract;
                }

                v7_1._abstract = v2_3;
                v14.article = v7_1;
            }

            if (v14.delete) {
                arg18.deleteArticle(v7_1);
                ArticleQueryThread.reportEvent("delete", arg19.groupId, null);
            } else {
                HttpHeader v2_10 = v6.getHeader("ETag");
                String v9_1 = null;
                if (v2_10 != null) {
                    v9_1 = v2_10.value();
                }

                long maxAge = NetworkUtils.extractMaxAge(v6);
                if (maxAge < 0) {
                    maxAge = 0;
                }
                arg18.insertDetail(v7_1, v14, v9_1, v3_2, v4_1, maxAge);
            }

            if ((v15 == 1 && v14.article != null && !android.support.design.a.isTextEmpty(v14.article.articleUrl)) || v15 != 1) {
                label_296:
                if (v15 != 0) {
                    return v14;
                }

                if (!android.support.design.a.isTextEmpty(v14.content)) {
                    return v14;
                }

                if (v14.imageDetail != null && !v14.imageDetail.isEmpty()) {
                    return v14;
                }

                v2_6 = new JSONObject();
                v2_6.put("error_type", 1);
                if (android.support.design.a.isTextEmpty(v14.content)) {
                    v2_6.put("error_msg", "nativeArticle with no content");
                } else {
                    v2_6.put("error_msg", "nativeGallery with no gallery");
                }
            } else {
                v2_6 = new JSONObject();
                v2_6.put("error_type", 1);
                v2_6.put("error_msg", "webContent with no content");
            }
            ArticleQueryThread.reportEvent("error", arg19.groupId, v2_6);
            return v14;
        }
        return null;
    }

    public static void b(List arg0) {
        b = arg0;
    }
}

